﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class FactoryReposity : Reposity
    {
        public IEnumerable<Factory> ListFactories()
        {
            var factories = from f in _dataContext.Factories
                            select f;
            foreach (Factory f in factories)
            {
                yield return f;
            }
        }

        public IEnumerable<Factory> QueryFactories(string name)
        {
            name = string.IsNullOrEmpty(name) ? "" : name;
            var factories = from f in _dataContext.Factories
                            where f.Name.Contains(name)
                            select f;
            foreach (Factory f in factories)
            {
                yield return f;
            }
        }

        public Factory GetFactoryById(long id)
        {
            var factory = from f in _dataContext.Factories
                          where id == f.Id
                          select f;

            return factory.First();
        }

        public Factory CreateFactory(Factory factory)
        {
            factory.CreateTime = DateTime.Now;
            factory.ModifyTime = DateTime.Now;
            _dataContext.Factories.InsertOnSubmit(factory);
            SubmitChanges();
            return factory;
        }

        public bool DeleteFactory(Factory factory)
        {
            _dataContext.Factories.DeleteOnSubmit(factory);
            SubmitChanges();
            return true;
        }

        public Factory EditFactory(Factory factory)
        {
            factory.ModifyTime = DateTime.Now;
            SubmitChanges();
            return factory;
        }
    }
}